package ui.activity.mine;

import adapter.AssessHighGradeAdapter;
import adapter.AssessMarkAdapter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.EvaluationBean;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.dispatchapp.andlei.R;
import com.marquee.dingrui.marqueeviewlib.MarqueeView;
import netWork.NetUtils;
import utils.DensityUtil;
import utils.GsonUtils;
import widget.MyAssessPopupWindow;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 我的评价 页面
 * @author Andlei
 * @date 2019/7/29.
 */
public class MyAssessActivity extends BaseLayoutActivity {
    private ImageView img_back;
    private TextView tv_months;
    private TextView tv_haoping_number,tv_chaping_number,tv_count;
    private RecyclerView recy_good_taglist,recy_bad_taglist,recy_high_grade;
    private AssessMarkAdapter mark_goodAdapter,mark_badAdapter;
    private List<EvaluationBean.TagInfoBean> list_good = new ArrayList<>();
    private List<EvaluationBean.TagInfoBean> list_bad = new ArrayList<>();
    private List<EvaluationBean.ReplyInfoBean> list_high = new ArrayList<>();
    private MarqueeView scrollTextView;
    private AssessHighGradeAdapter highGradeAdapter;
    private NetUtils netUtils;
    private TimePickerView pvCustomLunar;
    private PopupWindow popupWindow;
    private int r_type = 2;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_assess;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mContext, new NetUtils.NetRequestCallBack() {
            @Override
            public void success(String action, BaseBean t, Map tag) {
                if(action.equals("rider/comment-rider/rider-mine-evaluation")){
                    if(t.getCode()==200){
                        EvaluationBean bean = (EvaluationBean) GsonUtils.getInstance(mActivity).parseComplexJArrayByCommon(t,EvaluationBean.class);
                        tv_haoping_number.setText(bean.getEvaluation_count().getGood());
                        tv_chaping_number.setText(bean.getEvaluation_count().getBad());
                        list_good.clear();
                        list_bad.clear();
                        list_high.clear();
                        for(EvaluationBean.TagInfoBean tagInfoBean : bean.getTag_info()){
                            if(tagInfoBean.getScore().equals("1")){
                                list_bad.add(tagInfoBean);
                            }else {
                                list_good.add(tagInfoBean);
                            }
                        }
                        list_high.addAll(bean.getReply_info());
                        tv_count.setText("共"+list_high.size()+"条");
                        mark_goodAdapter.notifyDataSetChanged();
                        mark_badAdapter.notifyDataSetChanged();
                        highGradeAdapter.notifyDataSetChanged();
                    }else {
                        toast(t.getMsg());
                    }
                }
            }

            @Override
            public void error(String action, Throwable e, Map tag) {
            }
        });
        img_back = getView(R.id.img_back);
        tv_months = getView(R.id.tv_this_months);
        scrollTextView =  getView(R.id.scroll_tv);

       // tv_pingfen = getView(R.id.tv_pingfen);
        recy_good_taglist = getView(R.id.recy_good_taglist);
        recy_bad_taglist = getView(R.id.recy_bad_taglist);
        tv_haoping_number = getView(R.id.tv_haoping_number);
        tv_chaping_number = getView(R.id.tv_chaping_number);
        tv_count = getView(R.id.tv_count);
        recy_high_grade = getView(R.id.recy_high_grade);


    }

    @Override
    protected void formatViews() {
        img_back.setOnClickListener(this);
        tv_months.setOnClickListener(this);
        //开启滚动跑马灯
        scrollTextView.setContent("数据截止至昨日23:59，可能有延迟，请耐心等待");


        //好评
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,3);
        recy_good_taglist.setLayoutManager(gridLayoutManager);
        mark_goodAdapter = new AssessMarkAdapter(R.layout.item_assess_mark,list_good,1);
        mark_goodAdapter.bindToRecyclerView(recy_good_taglist);
        mark_goodAdapter.setEmptyView(R.layout.empty_good_or_bad_assess);
        recy_good_taglist.setAdapter(mark_goodAdapter);


        //差评
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(mContext,3);
        recy_bad_taglist.setLayoutManager(gridLayoutManager1);
        mark_badAdapter = new AssessMarkAdapter(R.layout.item_assess_mark,list_bad,2);
        mark_badAdapter.bindToRecyclerView(recy_bad_taglist);
        mark_badAdapter.setEmptyView(R.layout.empty_good_or_bad_assess);
        ((TextView)mark_badAdapter.getEmptyView().findViewById(R.id.tv_empty_text)).setText("本月未收到差评关键词");
        recy_bad_taglist.setAdapter(mark_badAdapter);


        //优质评价
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recy_high_grade.setLayoutManager(layoutManager);
        highGradeAdapter = new AssessHighGradeAdapter(R.layout.item_assess_high_grade,list_high);
        highGradeAdapter.bindToRecyclerView(recy_high_grade);
        highGradeAdapter.setEmptyView(R.layout.empty_good_or_bad_assess);
        ((TextView)highGradeAdapter.getEmptyView().findViewById(R.id.tv_empty_text)).setText("本月未收到评价内容");
        recy_high_grade.setAdapter(highGradeAdapter);

    }

    @Override
    protected void formatData() {
        initLunarPicker();
        loadData();
    }
    private void loadData(){
        Map<String,Object> map = new HashMap<>();
        map.put("r_type",""+r_type);
        netUtils.post("rider/comment-rider/rider-mine-evaluation",map,BaseBean.class,false);
    }
    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_this_months:
                popupWindow = new MyAssessPopupWindow(mContext, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (view.getId()){
                            case R.id.tv_open:
                                tv_months.setText("本周");
                                r_type = 1;
                                loadData();
                                break;
                            case R.id.tv_close:
                                tv_months.setText("本月");
                                r_type = 2;
                                loadData();
                                break;
                        }
                        popupWindow.dismiss();
                    }
                },false);
                popupWindow.showAtLocation(tv_months, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0, DensityUtil.dip2px(MyAssessActivity.this,75));
                break;
        }
    }
    /**
     * 时间初始化
     */
    private void initLunarPicker() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();

        startDate.set(Calendar.YEAR, startDate.get(Calendar.YEAR) - 1);

        pvCustomLunar = new TimePickerBuilder(mActivity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (v instanceof TextView) {
                    TextView tv = (TextView) v;
                    tv.setText(getTime(date,"MM")+"月");

                }
            }
        }).setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {

                    @Override
                    public void customLayout(final View v) {
                        final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomLunar.returnData();
                                pvCustomLunar.dismiss();
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomLunar.dismiss();
                            }
                        });
                    }
                })
                .setType(new boolean[]{true, true, false, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(Color.parseColor("#DDDDDD"))
                .build();
    }

    private String getTime(Date date,String fomat) {
        SimpleDateFormat format = new SimpleDateFormat(fomat);
        return format.format(date);
    }

}
